//
//  ZDForwardCell.swift
//  1026SwiftSinaWeiBo
//
//  Created by Apple on 15/11/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class ZDForwardCell: ZDStatusCell {
    
    
    override var status: ZDStatus? {
        
        didSet {
            
//            print("转发status：\(status)")
            
            if let retweeted_status = status?.retweeted_status {
                
                let name = retweeted_status.user?.name ?? "没有昵称"
                
                let text = retweeted_status.text ?? ""
                
//                 forwardLabel.text = "@\(name):\(text)"
                
                let nameAttribute = NSMutableAttributedString(string: "@\(name): ")
                
                let textAttribute = ZDEmoji.emojiStringToEmojiAttributeString(text, font: UIFont.systemFontOfSize(14))
                
                //拼接属性文本
                nameAttribute.appendAttributedString(textAttribute)
                //设置属性文本
                forwardLabel.attributedText = nameAttribute
            }
        }
    }

    
    override func prepareUI() {
        
        super.prepareUI()
        
        //添加子控件
        contentView.insertSubview(bkgButton, belowSubview: pictureView)  //插入到pictureView下面
        contentView.addSubview(forwardLabel)
        
        //布局约束
        bkgButton.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: nil, offset: CGPoint(x: -margin, y: margin))
        bkgButton.ff_AlignVertical(type: ff_AlignType.TopRight, referView: bottomView, size: nil, offset: CGPoint(x: 0, y: 0))
        
        
        forwardLabel.ff_AlignInner(type: ff_AlignType.TopLeft, referView: bkgButton, size: nil, offset: CGPoint(x: margin, y: margin))
        
        contentView.addConstraint(NSLayoutConstraint(item: forwardLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: UIScreen.width - 2 * margin))
        
        let pictureViewConstraints = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: forwardLabel, size: CGSize(width: 0, height: 0), offset: CGPoint(x: 0, y: margin))
        
        //保存宽约束
        pictureViewWidthC = pictureView.ff_Constraint(pictureViewConstraints, attribute: NSLayoutAttribute.Width)
        
        //保存高约束
        pictureViewHeightC = pictureView.ff_Constraint(pictureViewConstraints, attribute: NSLayoutAttribute.Height)
    }
    
    
    
    //MARK: - 懒加载
    lazy var forwardLabel: FFLabel = {
    
        let label = FFLabel(fontSize: 14, color: UIColor.lightGrayColor())
        
        //设置代理
        label.labelDelegate = self
        
        label.numberOfLines = 0
        
//        label.text = "测试label测试label测试label测试label测试label测试label测试label测试label测试label测试label测试label测试label测试label测试label"
        
        return label
    }()
    
    lazy var bkgButton: UIButton = {
    
        let btn = UIButton()
        
        btn.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        
        return btn
    }()
}


